package com.weimei.conversionTreasure.viewModel;

import android.app.Application;

import androidx.annotation.NonNull;

import com.gtdev5.geetolsdk.mylibrary.util.SpUtils;
import com.gtdev5.geetolsdk.mylibrary.util.Utils;
import com.weimei.conversionTreasure.activity.AboutActivity;
import com.weimei.conversionTreasure.activity.UserInfoActivity;
import com.weimei.conversionTreasure.app.AppConfig;
import com.weimei.conversionTreasure.http.entity.BaseResult;
import com.weimei.conversionTreasure.utils.UserUtils;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class SetVM extends TitleVM {
    public SetVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("设置");
    }

    public BindingCommand about = new BindingCommand(() -> startActivity(AboutActivity.class));

    public BindingCommand editInfo = new BindingCommand(() -> startActivity(UserInfoActivity.class));

    public BindingCommand outLogin = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            UserUtils.clear();
            ToastUtils.showShort("退出登录成功");
        }
    });

    public BindingCommand outOff = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (UserUtils.isLogin()){
//               logOff();
                SpUtils.getInstance().clear();
                UserUtils.clear();
                ToastUtils.showShort("注销成功");
            }

        }
    });



    private void logOff() {
        Map<String,String> map = new HashMap<>();
         map.put("user_id",UserUtils.getUserId());
        map.put("appname", AppConfig.APP_NAME);
        model.logOff(map)
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult baseResult) {
                        if (baseResult.issucc){
                            ToastUtils.showShort(baseResult.msg);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


}
